package com.sensePlusVR.activity.mine;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.main.R;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.BitmapUtil;
import com.sensePlusVR.utils.DensityUtil;

import java.util.ArrayList;
import java.util.LinkedList;

@SuppressLint("InflateParams")
@SuppressWarnings("deprecation")
public class UserGuideActivity extends BaseActivity implements OnClickListener {
	private ImageButton title_bar_left_ib;

	private TextView title_bar_title_tv;
	
    //引导图片资源  
    private static final int[] pics = {R.drawable.userguide04, R.drawable.userguide01,  
            R.drawable.userguide02, R.drawable.userguide03};  

	private ViewPager viewPager;

	/** 装分页显示的view的数组 */
	private ArrayList<View> pageViews;

	/** 将小圆点的图片用数组表示 */
	private ImageView[] imageViews;

	// 包裹滑动图片的LinearLayout
	private ViewGroup viewPics;

	// 包裹小圆点的LinearLayout
	private ViewGroup viewPoints;
	
	@Override
	public void setMyContentView() {
		//此类不用注解xml
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		LayoutInflater inflater = getLayoutInflater();
		//存放图片数据
		pageViews = new ArrayList<View>();
		//存放小圆点数据
		imageViews = new ImageView[pics.length];
		// 从指定的XML文件加载视图
		viewPics = (ViewGroup) inflater.inflate(R.layout.activity_setting_user_guide, null);
		
		//顶部导航监听
		title_bar_title_tv = (TextView) viewPics.findViewById(R.id.title_bar_title_tv);
		title_bar_left_ib = (ImageButton) viewPics.findViewById(R.id.title_bar_left_ib);
		AppManager.getInstance().addActivity(this);
		title_bar_title_tv.setText(R.string.setting_guide);
		title_bar_left_ib.setOnClickListener(this);

		viewPoints = (ViewGroup) viewPics.findViewById(R.id.viewGroup);
		viewPager = (ViewPager) viewPics.findViewById(R.id.guidePages);
		
		LinearLayout.LayoutParams pointLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,  
	             LinearLayout.LayoutParams.WRAP_CONTENT);  
		//设置原点左右边距
		int marginPX = DensityUtil.dip2px(this, 4.5f);
		// 添加小圆点的图片
		for (int i = 0; i < pics.length; i++) {
			//设置图片(为了防止多个图片同时加载到viewpager中造成的OOM,所以在GuidePageAdapter的instantiateItem中setImageBitmap)
			ImageView ivPic = new ImageView(this);  
			pageViews.add(ivPic);
			//设置小圆点
			ImageView ivPoint = new ImageView(UserGuideActivity.this);
			// 两个圆点之间的距离
			pointLP.leftMargin = marginPX;
			pointLP.rightMargin = marginPX;
			ivPoint.setLayoutParams(pointLP);
			imageViews[i] = ivPoint;
			if (i == 0) {
				imageViews[i]
						.setBackgroundResource(R.drawable.user_guide_point_s);
			} else {
				imageViews[i]
						.setBackgroundResource(R.drawable.user_guide_point_n);
			}

			viewPoints.addView(imageViews[i]);
		}

		setContentView(viewPics);

		viewPager.setAdapter(new GuidePageAdapter());
		viewPager.setOnPageChangeListener(new GuidePageChangeListener());
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_bar_left_ib:
			AppManager.getInstance().finishActivity(this);
			break;
		}
	}

	class GuidePageAdapter extends PagerAdapter {
		
		 private final LinkedList<View> recyleBin=new LinkedList<View>();

		// 销毁position位置的界面
		@Override
		public void destroyItem(View v, int position, Object arg2) {
			((ViewPager) v).removeView(pageViews.get(position));

		}

		@Override
		public void finishUpdate(View arg0) {}

		// 获取当前窗体界面数
		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public Object instantiateItem(View v, int position) {
			//http://www.bubuko.com/infodetail-799384.html
            //在此设置背景图片，提高加载速度，解决OOM问题
             View view;
             int count=getCount();
             if(!recyleBin.isEmpty()) {
                 view=recyleBin.pop();
             }else {
                 view=pageViews.get(position);
                 ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                         ViewGroup.LayoutParams.WRAP_CONTENT,
                         ViewGroup.LayoutParams.WRAP_CONTENT);
                ((ImageView) view).setImageBitmap(BitmapUtil.getBitmapFromResID(UserGuideActivity.this, pics[position % count]));
                view.setLayoutParams(params);
             }
             ((ViewPager) v).addView(view,0);
             return pageViews.get(position);
		}

		// 判断是否由对象生成界面
		@Override
		public boolean isViewFromObject(View v, Object arg1) {
			return v == arg1;
		}

		@Override
		public void startUpdate(View arg0) {

		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {}

		@Override
		public Parcelable saveState() {
			return null;
		}
	}

	class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {}

		@Override
		public void onPageSelected(int position) {
			for (int i = 0; i < imageViews.length; i++) {
				imageViews[position]
						.setBackgroundResource(R.drawable.user_guide_point_s);
				// 不是当前选中的page，其小圆点设置为未选中的状态
				if (position != i) {
					imageViews[i]
							.setBackgroundResource(R.drawable.user_guide_point_n);
				}
			}

		}
	}

}
